package com.ymt.bpm.engine.listener;

import com.ymt.bpm.engine.listener.engine.exception.EngineException;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by Johnny on 2016/12/23.
 * Common activiti event listener
 */
@Component
public class CommonEventListener implements ActivitiEventListener {

    /**
     * Logger
     */
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /*private static ActivitiEventListener instance;

    public static ActivitiEventListener getInstance() {
        if (instance==null) {
            instance = new ActivitiEventListener();
        }
        return instance;
    }*/

    @Autowired
    private CommonEventDispatcher dispatcher;

    @Override
    public void onEvent(ActivitiEvent activitiEvent) {
        logger.debug("onEvent event: "+activitiEvent.getType());
        try {
            dispatcher.dispatch(activitiEvent);
        } catch (EngineException e) {
            e.printStackTrace();
            throw new RuntimeException(e);  //for transaction rollback
        }
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }

}
